package com.cskaoyan.wordmemorize.controller.app;

import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.app.*;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserRemindCommand;
import com.cskaoyan.wordmemorize.service.GoodsService;
import com.cskaoyan.wordmemorize.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@Tag(name = "移动端-用户(我的)模块")
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    GoodsService goodsService;

    @Resource
    UserService userService;

    @Operation(summary = "***获取用户信息",
            description = "在用户打开APP或者其他路由跳转的时候，通过token获取用户信息",
            responses = {
                    @ApiResponse(
                            responseCode = "200",
                            description = "成功. 返回登录成功的用户信息，以及当前等级权限码集合"
                    ),
                    @ApiResponse(responseCode = "500",
                            description = "请求失败"
                    )
            }
    )
    @GetMapping("info")
    public Result<UserDTO> getUserInfo() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        UserDTO userInfo = userService.getUserInfo(userId);
        return Result.ok(userInfo);
    }

    //返回用户页面展示条目的信息
    @GetMapping("/voc/my/page")
    public Result<UserPageDTO> getUserPage() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        UserPageDTO userPage = userService.getUserPage(userId);
        return Result.ok(userPage);
    }

    //返回用户的单词上限统计数据
    @GetMapping("/voc/bound/statistics")
    public Result<UserVocBoundStatisticsDTO> getUserVocBoundStatistics() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        UserVocBoundStatisticsDTO statistics = userService.getUserVocBoundStatistics(userId);
        return Result.ok(statistics);
    }

    //返回用户的单词上限变化明细,分页
    @GetMapping("/voc/bound/detail")
    public Result<PageDTO<DisplayUserVocBoundLogDTO>> getUserVocBoundLog(PageRequest pageRequest) {
        Long userId = StpKit.USER.getLoginIdAsLong();
        PageDTO<DisplayUserVocBoundLogDTO> userVocBoundLog = userService.getUserVocBoundLog(pageRequest, userId);
        return Result.ok(userVocBoundLog);
    }

    //返回用户的升级信息
    @GetMapping("/upgrade/info")
    public Result<NextLevelDTO> getUserNextDTO() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        NextLevelDTO nextLevel = userService.getUserNextDTO(userId);
        return Result.ok(nextLevel);
    }

    //返回用户的等级信息
    @GetMapping("/level/info")
    public Result<DisplayUserLevelDTO> getUserLevelInfo() {
        Long userId = StpKit.USER.getLoginIdAsLong();
        DisplayUserLevelDTO userLevelInfo = userService.getUserLevelInfo(userId);
        return Result.ok(userLevelInfo);
    }

    //修改用户的提醒信息时间
    @PutMapping("remind")
    public Result<UserRemindDTO> updateUserRemind(@RequestBody UserRemindCommand command) {
        //获取ID
        //输出command的值
        System.out.println(command);

        Long userId = StpKit.USER.getLoginIdAsLong();
        command.setUserId(userId.toString());
        userService.updateUserRemind(command);
        UserRemindDTO userRemind = userService.getUserRemind(userId);
        return Result.ok(userRemind);
    }

    //修改用户的每日学习量
    @PutMapping("day/voc")
    public Result<UserDTO> updateUserVocCountOfDay(@RequestParam Integer count) {
        Long userId = StpKit.USER.getLoginIdAsLong();
        userService.updateVocCountOfDay(userId, count);
        UserDTO userInfo = userService.getUserInfo(userId);
        return Result.ok(userInfo);
    }
}
